package com.xy.modular.shop.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.shop.entity.ProductSku;
import com.xy.modular.shop.service.IProductSkuService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 8. 商品库存量表 product_sku 此表为关键表，根据商品的属性和属性值来判定商品的价格和库存量 前端控制器
 * </p>
 *
 * @author BCK
 * @since 2019-10-12
 */
@Controller
@RequestMapping("/shop/product-sku")
public class ProductSkuController extends BaseController {
	static Logger log = LoggerFactory.getLogger(ProductSkuController.class);

	@Autowired
	private IProductSkuService productSkuService;

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject listModule(ProductSku productSku, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, productSkuService.findColumnsPageAll(pageRequest),
				productSkuService.getCount(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(ProductSku productSku) {
		try {
			productSkuService.validateEntity(productSku);
			productSku.setId(UUIDUtils.UUID());
			productSkuService.save(productSku);
		} catch (Exception e) {
			log.info("ProductSku保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(ProductSku productSku) {
		try {
			productSkuService.validateEntity(productSku);
			productSkuService.update(productSku);
		} catch (Exception e) {
			log.info("ProductSku修改失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(@PathVariable String id) {
		try {
			productSkuService.deleteById(id);
		} catch (Exception e) {
			log.info("ProductSku删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
